﻿using EWSEditor.Forms;
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EWSEditor.CRM
{
    public class FormUtility
    {

        public static FolderTreeForm TreeForm;

        public static void BindAttributes(Control topcontrol, Entity entity, string entityname)
        {
            foreach (Control control in topcontrol.Controls)
            {
                BindAttributes(control, entity, entityname);
            }
            if (topcontrol.Name.StartsWith("txtbx"))
            {
                string formattribute = topcontrol.Name.Substring(5);
                if (entity.Attributes.Contains(formattribute))
                {
                    KeyValuePair<string, object> keyvaluepair = (from A in entity.Attributes where A.Key == formattribute select A).Single();
                    CastAndSetValue(topcontrol, keyvaluepair, entityname);
                }

            }
        }
        private static void CastAndSetValue(Control ctrl, KeyValuePair<string, object> entityattribute, string entityname)
        {
            var valueasstring = entityattribute.Value as string;

            if (valueasstring != null)
            {
                ctrl.Text = (string)entityattribute.Value;
                return;
            }

            var valueasentityreference = entityattribute.Value as EntityReference;

            if (valueasentityreference != null)
            {
                ctrl.Text = ((EntityReference)entityattribute.Value).Id.ToString();
                return;
            }

            if (entityattribute.Value.GetType() == typeof(int))
            {
                ctrl.Text = ((int)entityattribute.Value).ToString();
                return;
            }

            if (entityattribute.Value.GetType() == typeof(DateTime))
            {
                ctrl.Text = ((DateTime)entityattribute.Value).ToString();
                return;
            }

            if (entityattribute.Value.GetType() == typeof(Guid))
            {
                ctrl.Text = ((Guid)entityattribute.Value).ToString();
                return;
            }

            if (entityattribute.Value.GetType() == typeof(bool))
            {
                ctrl.Text = ((bool)entityattribute.Value).ToString();
                return;
            }

            if (entityattribute.Value.GetType() == typeof(OptionSetValue))
            {
                string value = MetadataHelpers.GetOptionSetValueLabel(entityname, entityattribute.Key, ((OptionSetValue)entityattribute.Value).Value);
                ctrl.Text = value;
                return;
            }

        }

        private static void SetValue(Control ctrl, string value)
        {
            StringBuilder sb = new StringBuilder(ctrl.Text);
            if (ctrl.Text.Length > 0)
            {
                sb.Append(" ").Append(value);
            }
            else
            {
                sb.Append(value);
            }
            ctrl.Text = sb.ToString();

        }

        public static void ShowError(string Exception, string Message)
        {
            MessageBox.Show(Exception, Message);
        }
    }
}
